import {ajax,getUserInfo} from '@/common/common.js'
import {bpmUrl} from "@/common/config.js"
//初始赋值
function  formonload(list,formData,bussiness){
	let currentNodeid = bussiness.currentNodeid
	let arr = list.map(item => {
		if(formData.BudgetCode){
			if([
				'BudgetName',
				'CBZX',
				'BudgetItem',
				'MapBudget',
				].indexOf(item.name) != 1){
				item.disabled = true
			}
		}
		let WF_DocStatus = bussiness.docStatus
		if(WF_DocStatus=="Draft"){
		    if(formData.BudgetCode){
		        fznext1(formData,list);
		    }
		}
		return item
	})
	if(currentNodeid=="T10002"){
		// 初始化加载预算名称
		initListOption(list,'BudgetCode',bussiness);
	}
	return arr
}
// 初始化加载预算名称
function initListOption(list,name,bussiness){
	let Processid = bussiness.processid
	let user = getUserInfo()
	let paramas = {
		paramasMap: {
			Processid:Processid
		},
		headersMap: {
			sysid: "bpm",
			syspwd: "pass",
			userid: user.bpmUid
		},
		url: bpmUrl + '/rest/appform/appform/R_appform_CRMVR_B122'
	}
		ajax({
			url:'/bpm/get',
			method: 'POST',
			data: paramas,
			success:res => {
				let arr = res.data.data
				arr = arr.map(item => {
					item.label = item.text
					return item
				})
				list.map(item => {
					if(item.name == name){
						item.optiona.data = arr
					}
				})
			}
		})
}

//给预算号相关字段赋值
function fz1(item,formData,list){
    //获取当前选中的预算名称
    let BudgetCode = formData.BudgetCode
	let BudgetName = formData.BudgetName
	//预算号赋值
	let user = getUserInfo()
	let paramas = {
		paramasMap: {
			BudgetCode:BudgetCode,
			BudgetName:BudgetName
		},
		headersMap: {
			sysid: "bpm",
			syspwd: "pass",
			userid: user.bpmUid
		},
		url: bpmUrl + '/rest/appform/appform/R_appform_CRMVR_B123'
	}
	ajax({
		url:'/bpm/get',
		method: 'POST',
		data: paramas,
		success:res => {
			formData.BudgetName = res.data.data.BudgetName
		}
	})
    
    //成本中心赋值
	let CBZX = formData.CBZX
	paramas.paramasMap = {
		BudgetName:BudgetName,
		BudgetCode:BudgetCode,
		CBZX:CBZX
	}
	paramas.url = bpmUrl + '/rest/appform/appform/R_appform_CRMVR_B117'
	ajax({
		url:'/bpm/get',
		method: 'POST',
		data: paramas,
		success:res => {
			formData.CBZX = res.data.data.CBZX
		}
	})
    //预算项目赋值
	let BudgetItem = formData.BudgetItem
	paramas.paramasMap = {
		BudgetName:BudgetName,
		BudgetCode:BudgetCode,
		BudgetItem:BudgetItem
	}
	paramas.url = bpmUrl + '/rest/appform/appform/R_appform_CRMVR_B118'
	ajax({
		url:'/bpm/get',
		method: 'POST',
		data: paramas,
		success:res => {
			formData.BudgetItem = res.data.data.BudgetItem
		}
	})
    //预算号对应预算项目
	let MapBudget = formData.MapBudget
	paramas.paramasMap = {
		BudgetName:BudgetName,
		BudgetCode:BudgetCode,
		MapBudget:MapBudget
	}
	paramas.url = bpmUrl + '/rest/appform/appform/R_appform_CRMVR_B119'
	ajax({
		url:'/bpm/get',
		method: 'POST',
		data: paramas,
		success:res => {
			formData.MapBudget = res.data.data.MapBudget
		}
	})
	
	list.map(item => {
		if(['BudgetName',
			'CBZX',
			'BudgetItem',
			'MapBudget',
			].indexOf(item.name) != -1){
			item.disabled = true // 设置字段disabled
		}
		return item
	})
}


function formonsubmit(list,formData,bussiness){
	let flag = true
	let currentNodeid = bussiness.currentNodeid
	if(currentNodeid == "T10002"){
		
	}
	list = list.map(item => {
		if(['BudgetName',
			'CBZX',
			'BudgetItem',
			'MapBudget'].indexOf(item.name) != -1){
			item.disabled = false
		}
		return item
	})
	return flag;
}

//模糊匹配预算名称
function getItem(queryString,formData, cb) {
	console.log(queryString, cb)
	let user = getUserInfo()
	let paramas = {
		paramasMap: {
			BudgetCode: queryString
		},
		headersMap: {
			sysid: "bpm",
			syspwd: "pass",
			userid: user.bpmUid
		},
		url: bpmUrl + '/rest/appform/CRMVR/R_CRMVR_B074'
	}
	ajax({
		url: '/bpm/get',
		method: 'POST',
		data: paramas,
		success: res => {
			console.log(res)
			let data = res.data.data.map(item => {
				item.id = item.BudgetCode
				item.name = item.BudgetName
				return item
			})
			console.log(data)
			cb(data)
		}
	})
}

//给预算号相关字段再次赋值
function fznext1(formData,list){
	let BudgetCode = formData.BudgetCode
	let BudgetName = formData.BudgetName
	let paramas = {
		paramasMap: {
			BudgetCode:BudgetCode,
			BudgetName:BudgetName
		},
		headersMap: {
			sysid: "bpm",
			syspwd: "pass",
			userid: user.bpmUid
		},
		//"rule?wf_num=R_CRMVR_B123&BudgetCode="+BudgetCode+"" 接口文档无此接口
		url: bpmUrl + '/rest/appform/appform/R_appform_CRMVR_B123'
	}
	ajax({
		url:'/bpm/get',
		method: 'POST',
		data: paramas,
		success:res => {
			formData.BudgetName = res.data.data
		}
	})
	//成本中心赋值
	let CBZX = formData.CBZX
	paramas = {
		paramasMap: {
			BudgetCode:BudgetCode,
			BudgetName:BudgetName,
			CBZX:CBZX
		},
		headersMap: {
			sysid: "bpm",
			syspwd: "pass",
			userid: user.bpmUid
		},
		//"rule?wf_num=R_CRMVR_B117&BudgetName="+BudgetName+"&BudgetCode="+BudgetCode+"" 接口文档无此接口
		url: bpmUrl + '/rest/appform/appform/R_appform_CRMVR_B117'
	}
	ajax({
		url:'/bpm/get',
		method: 'POST',
		data: paramas,
		success:res => {
			formData.CBZX = res.data.data
		}
	})
	
	//预算项目赋值
	let BudgetItem = formData.BudgetItem
	paramas = {
		paramasMap: {
			BudgetCode:BudgetCode,
			BudgetName:BudgetName,
			BudgetItem:BudgetItem
		},
		headersMap: {
			sysid: "bpm",
			syspwd: "pass",
			userid: user.bpmUid
		},
		//"rule?wf_num=R_CRMVR_B118&BudgetName="+BudgetName+"&BudgetCode="+BudgetCode+"" 接口文档无此接口
		url: bpmUrl + '/rest/appform/appform/R_appform_CRMVR_B118'
	}
	ajax({
		url:'/bpm/get',
		method: 'POST',
		data: paramas,
		success:res => {
			formData.BudgetItem = res.data.data
		}
	})
	//预算号对应预算项目
	let MapBudget = formData.MapBudget
	paramas = {
		paramasMap: {
			BudgetCode:BudgetCode,
			BudgetName:BudgetName,
			MapBudget:MapBudget
		},
		headersMap: {
			sysid: "bpm",
			syspwd: "pass",
			userid: user.bpmUid
		},
		//"rule?wf_num=R_CRMVR_B119&BudgetName="+BudgetName+"&BudgetCode="+BudgetCode+"" 接口文档无此接口
		url: bpmUrl + '/rest/appform/appform/R_appform_CRMVR_B119'
	}
	ajax({
		url:'/bpm/get',
		method: 'POST',
		data: paramas,
		success:res => {
			formData.MapBudget = res.data.data
		}
	})
	
	list.map(item => {
		if(['BudgetName',
			'CBZX',
			'BudgetItem',
			'MapBudget',
			].indexOf(item.name) != -1){
			item.disabled = true // 设置字段disabled
		}
		return item
	})
}

export default {
	formonload,
	fz1,
	getItem,
	formonsubmit,
}